﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Aribaccio;
using Aribaccio.Device;
using BDPAutodetect.Device;
using BDPAutodetect.Hardware;

namespace BDPAutodetect.Simcore
{
    public class DeviceSimulatorManager
    {
        /// <summary>
        /// 设备管理实例
        /// </summary>
        protected DeviceManager DeviceMgr { get; set; }
        /// <summary>
        /// 检测设备集合
        /// </summary>
        public List<DeviceSimulatorBase> DeviceSimulators { get; protected set; }

        /// <summary>
        /// 构造函数[注入]
        /// </summary>
        public DeviceSimulatorManager(DeviceManager argDeviceManager)
        {
            this.DeviceMgr = argDeviceManager;
            this.DeviceSimulators = new List<DeviceSimulatorBase>();
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <returns></returns>
        public void Initialize()
        {
            if (this.DeviceMgr.Count <= 0) return;
            //构建检测设备
            foreach (var deviceItem in this.DeviceMgr.Devices)
            {
                var deviceType = deviceItem.DevicePara.DeviceType.ToEnum<DeviceType>();
                switch (deviceType)
                {
                    case DeviceType.Customer:
                        this.DeviceSimulators.Add(new DeviceSimulatorCustomer(deviceItem.DevicePara.ModuleType, (DeviceSerialAscii<IDevicePara>)deviceItem));
                        break;
                    case DeviceType.Electrical:
                        this.DeviceSimulators.Add(new DeviceSimulatorElectrical((DeviceSerialModbus<IDevicePara>)deviceItem));
                        break;
                    case DeviceType.SensorSign:
                        this.DeviceSimulators.Add(new DeviceSimulatorSensorSign((DeviceSerialModbus<IDevicePara>)deviceItem));
                        break;
                }
            }
            //所有检测设备初始化
            foreach (var item in this.DeviceSimulators)
            {
                item.Initialize();
            }
        }

        /// <summary>
        /// 获取检测设备
        /// </summary>
        /// <param name="argDeviceType">设备类型</param>
        /// <returns></returns>
        public DeviceSimulatorBase? GetDetectDevice(DeviceType argDeviceType)
        {
            if (this.DeviceSimulators == null || this.DeviceSimulators.Count <= 0) return null;
            return this.DeviceSimulators.FirstOrDefault(x => x.DeviceType == argDeviceType);
        }

        /// <summary>
        /// 获取检测设备
        /// </summary>
        /// <param name="argDeviceType">设备类型</param>
        /// <param name="argDeviceModule">设备模组类型</param>
        /// <returns></returns>
        public DeviceSimulatorBase? GetDetectDevice(DeviceType argDeviceType, DeviceModule argDeviceModule)
        {
            if (this.DeviceSimulators == null || this.DeviceSimulators.Count <= 0) return null;
            return this.DeviceSimulators.FirstOrDefault(x => x.DeviceType == argDeviceType && x.DeviceModule == argDeviceModule);
        }
    }
}
